package com.ja.openchat.viewmodels;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ja.openchat.ui.me.MemoryDosageActivity;
import com.ja.openchat.ui.me.SettingManager;
import com.ja.openchat.ui.me.UseMobileNetworkActivity;
import com.zy.openchat.Constants;
import com.zy.openchat.core.model.setting.LocalSettingEvent;
import com.zy.openchat.util.SPUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;


public class DataStorageViewModel extends BaseViewModel {
    public ObservableField<String> mobileNetworkLables = new ObservableField<>("");
    public ObservableField<String> wifiNetworkLables = new ObservableField<>("");

    public ObservableField<Boolean> autoAutoSaveImage = new ObservableField<>(true);
    public ObservableField<Boolean> autoAutoSaveEditImage = new ObservableField<>(true);
    public ObservableField<Boolean> autoAutoPlayGif = new ObservableField<>(true);

    public BindingCommand toggleAutoSaveImage = new BindingCommand(() -> {
        boolean isChecked = !autoAutoSaveImage.get();
        SPUtils.put(getApplication(),Constants.SPU_FOR_AUTO_SAVE_IMAGE,isChecked);
        autoAutoSaveImage.set(isChecked);
    });

    public BindingCommand toggleAutoSaveEditImage = new BindingCommand(() -> {
        boolean isChecked = !autoAutoSaveEditImage.get();
        SPUtils.put(getApplication(),Constants.SPU_FOR_AUTO_SAVE_EDIT_IMAGE,isChecked);
        autoAutoSaveEditImage.set(isChecked);
    });

    public BindingCommand togglAutoPlayGif = new BindingCommand(() -> {
        boolean isChecked = !autoAutoPlayGif.get();
        SPUtils.put(getApplication(),Constants.SPU_FOR_AUTO_PLAY_GIF,isChecked);
        autoAutoPlayGif.set(isChecked);
        SettingManager.getInstance().initPlayGif();
    });

    private final String SPE_LABLE = "/";

    public DataStorageViewModel(@NonNull Application application) {
        super(application);

        mobileNetworkLables.set(getNetworkValue(Constants.SPU_FOR_USE_MOBILE_NETWORK));
        wifiNetworkLables.set(getNetworkValue(Constants.SPU_FOR_USE_WIFI_NETWORK));
        autoAutoSaveImage.set(SPUtils.getBoolean(getApplication(),Constants.SPU_FOR_AUTO_SAVE_IMAGE,true));
        autoAutoSaveEditImage.set(SPUtils.getBoolean(getApplication(),Constants.SPU_FOR_AUTO_SAVE_EDIT_IMAGE,true));
        autoAutoPlayGif.set(SPUtils.getBoolean(getApplication(),Constants.SPU_FOR_AUTO_PLAY_GIF,true));
    }

    private String getNetworkValue(String networkType){
        String mobileValue = SPUtils.getString(getApplication(), networkType,UseMobileNetworkActivity.ALL_OPEN);
        String mobileShowValue = "";
        if(!TextUtils.isEmpty(mobileValue)){
            if(mobileValue.contains(String.valueOf(Constants.NETWORK_FOR_IMAGE))){
                mobileShowValue = "图片";
            }
            if(mobileValue.contains(String.valueOf(Constants.NETWORK_FOR_VIDEO))){
                mobileShowValue = mobileShowValue+(TextUtils.isEmpty(mobileShowValue)?"":SPE_LABLE)+"视频";
            }
            if(mobileValue.contains(String.valueOf(Constants.NETWORK_FOR_FILE))){
                mobileShowValue = mobileShowValue+(TextUtils.isEmpty(mobileShowValue)?"":SPE_LABLE)+"文件";
            }
        }
        return mobileShowValue;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLocalSettingEvent(final LocalSettingEvent tg_event) {
        if(tg_event==null|| TextUtils.isEmpty(tg_event.SpuKey)){
            return;
        }
        if(Constants.SPU_FOR_USE_MOBILE_NETWORK.equals(tg_event.SpuKey)){
            mobileNetworkLables.set(getNetworkValue(Constants.SPU_FOR_USE_MOBILE_NETWORK));
        }else if(Constants.SPU_FOR_USE_WIFI_NETWORK.equals(tg_event.SpuKey)){
            wifiNetworkLables.set(getNetworkValue(Constants.SPU_FOR_USE_WIFI_NETWORK));
        }
    }



    public BindingCommand btnOnClickMobileNetwork = new BindingCommand(() -> {
        startActivity(UseMobileNetworkActivity.class,UseMobileNetworkActivity.newInstance(0));
    });

    public BindingCommand btnOnClickWifiNetwork = new BindingCommand(() -> {
        startActivity(UseMobileNetworkActivity.class,UseMobileNetworkActivity.newInstance(1));
    });

    public BindingCommand memoryDosageClick = new BindingCommand(() -> {
        startActivity(MemoryDosageActivity.class);
    });
}
